﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CCWin;
using ModbusTest.Config;

namespace ModbusTest
{
    public partial class FrmDisplay : Form
    {

        public ModuleConfig _cfg;
        public int PLCLocation;
        List<SaveItem> sis = new List<SaveItem>();
        public FrmDisplay()
        {
            InitializeComponent();
        }
        public string OKQty { get{return lblOKqty.Text;}set{lblOKqty.Text=value;} }
        public string NGQty { get { return lblNgQty.Text; } set {lblNgQty.Text = value; } }
        public string TotalQty { get { return this.lblTotal.Text; } set { this.lblTotal.Text = value; } }

        public string OKRate { get { return this.lblOKRate.Text; } set {  this.lblOKRate.Text = value; } }
        public string NGRate { get { return this.lblNgRate.Text; } set { this.lblNgRate.Text = value; } }

        public string TestItem1 { get {return lblTestItem1.Text;} set{lblTestItem1.Text=value;} }
        public string TestItem1NGQty { get {return this.lblItem1NgQty.Text;} set{lblItem1NgQty.Text=value;} }
        public string TestItem1NGRate { get {return this.lblItem1NGRate .Text;} set{this.lblItem1NGRate.Text=value;} }

        public string TestItem2 { get {return lblTestItem2.Text;} set{lblTestItem2.Text=value;} }
        public string TestItem2NGQty { get { return this.lblItem2NgQty.Text; } set { lblItem2NgQty.Text = value; } }
        public string TestItem2NGRate { get { return this.lblItem2NGRate.Text; } set { this.lblItem2NGRate.Text = value; } }

        public string TestItem3 { get { return lblTestItem3.Text; } set { lblTestItem3.Text = value; } }
        public string TestItem3NGQty { get { return this.lblItem3NgQty.Text; } set { lblItem3NgQty.Text = value; } }
        public string TestItem3NGRate { get { return this.lblItem3NGRate.Text; } set { this.lblItem3NGRate.Text = value; } }

        public string Header1 { get { return this.dataGridView1.Columns[1].HeaderText; } set { this.dataGridView1.Columns[1].HeaderText = value; } }
        public string Header2 { get { return this.dataGridView1.Columns[3].HeaderText; } set { this.dataGridView1.Columns[3].HeaderText=value; } }
        public string Header3 { get { return this.dataGridView1.Columns[5].HeaderText; } set { this.dataGridView1.Columns[5].HeaderText=value; } }
        public string Header3Value { get { return this.dataGridView1.Columns[6].HeaderText; } set { this.dataGridView1.Columns[6].HeaderText = value; } }
        public string lvHeader1 { get { return skinListView1.Items[0].Text; } set { skinListView1.Items[0].Text = value; } }
        public string lvHeader2 { get { return skinListView1.Items[0].SubItems[2].Text; } set { skinListView1.Items[0].SubItems[2].Text = value; } }
        public string lvHeader3 { get { return skinListView1.Items[0].SubItems[4].Text; } set { skinListView1.Items[0].SubItems[4].Text = value; } }

        public string lvHeader1Value { get { return skinListView1.Items[0].SubItems[1].Text; } set { skinListView1.Items[0].SubItems[1].Text = value; } }
        public string lvHeader2Value { get { return skinListView1.Items[0].SubItems[3].Text; } set { skinListView1.Items[0].SubItems[3].Text = value; } }
        public string lvHeader3Value { get { return skinListView1.Items[0].SubItems[4].Text; } set { skinListView1.Items[0].SubItems[5].Text = value; } }

        public string lvLowerOffsetValue1 { get { return skinListView1.Items[1].SubItems[1].Text; } set { skinListView1.Items[1].SubItems[1].Text = value; } }
        public string lvLowerOffsetValue2 { get { return skinListView1.Items[1].SubItems[3].Text; } set { skinListView1.Items[1].SubItems[3].Text = value; } }
        public string lvLowerOffsetValue3 { get { return skinListView1.Items[1].SubItems[5].Text; } set { skinListView1.Items[1].SubItems[5].Text = value; } }

        public string lvLowerOffsetHeader1 { get { return skinListView1.Items[1].SubItems[0].Text; } set { skinListView1.Items[1].SubItems[0].Text = value; } }
        public string lvLowerOffsetHeader2 { get { return skinListView1.Items[1].SubItems[2].Text; } set { skinListView1.Items[1].SubItems[2].Text = value; } }
        public string lvLowerOffsetHeader3 { get { return skinListView1.Items[1].SubItems[4].Text; } set { skinListView1.Items[1].SubItems[4].Text = value; } }

        public string lvUpperOffsetValue1 { get { return skinListView1.Items[2].SubItems[1].Text; } set { skinListView1.Items[2].SubItems[1].Text = value; } }
        public string lvUpperOffsetValue2 { get { return skinListView1.Items[2].SubItems[3].Text; } set { skinListView1.Items[2].SubItems[3].Text = value; } }
        public string lvUpperOffsetValue3 { get { return skinListView1.Items[2].SubItems[5].Text; } set { skinListView1.Items[2].SubItems[5].Text = value; } }

        public string lvUpperOffsetHeader1 { get { return skinListView1.Items[2].SubItems[0].Text; } set { skinListView1.Items[2].SubItems[0].Text = value; } }
        public string lvUpperOffsetHeader2 { get { return skinListView1.Items[2].SubItems[2].Text; } set { skinListView1.Items[2].SubItems[2].Text = value; } }
        public string lvUpperOffsetHeader3 { get { return skinListView1.Items[2].SubItems[4].Text; } set { skinListView1.Items[2].SubItems[4].Text = value; } }

        public string MyProductName { get { return this.lblProductName.Text; } set {this.lblProductName.Text=value;} }

        public void UpadateSubItemNGRates(List<SubItemNGRate> ngrates)
        {
            if (ngrates.Count == 1)
            {
                TestItem1 = ngrates[0].Name;
                TestItem1NGQty = ngrates[0].NgQty;
                TestItem1NGRate = ngrates[0].NgRate;

                TestItem2 = "";
                TestItem2NGQty = "";
                TestItem2NGRate = "";

                TestItem3 = "";
                TestItem3NGQty = "";
                TestItem3NGRate = "";
            }
            else if (ngrates.Count == 2)
            {
                TestItem1 = ngrates[0].Name;
                TestItem1NGQty = ngrates[0].NgQty;
                TestItem1NGRate = ngrates[0].NgRate;

                TestItem2 = ngrates[1].Name;
                TestItem2NGQty = ngrates[1].NgQty;
                TestItem2NGRate = ngrates[1].NgRate;

                TestItem3 = "";
                TestItem3NGQty = "";
                TestItem3NGRate = "";           
            }
            else if (ngrates.Count == 3)
            {
                TestItem1 = ngrates[0].Name;
                TestItem1NGQty = ngrates[0].NgQty;
                TestItem1NGRate = ngrates[0].NgRate;

                TestItem2 = ngrates[1].Name;
                TestItem2NGQty = ngrates[1].NgQty;
                TestItem2NGRate = ngrates[1].NgRate;

                TestItem3 = ngrates[2].Name;
                TestItem3NGQty = ngrates[2].NgQty;
                TestItem3NGRate = ngrates[2].NgRate;
            }
        }
        public void Updatedgv(SaveItem si)
        {
            if (sis.Count == 512)
            {
                sis.RemoveAt(511);
            }
            sis.Insert(0,si);
            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.DataSource = new BindingList<SaveItem>(sis);
            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                for (int i = 0; i < row.Cells.Count; i++)
                {
                    if (row.Cells[i].Value.ToString().Contains("NG"))
                    {
                        row.Cells[i].Style.ForeColor = Color.Red;
                    }
                    else if (row.Cells[i].Value.ToString().Contains("OK"))
                    {
                        row.Cells[i].Style.ForeColor = Color.Green;
                    }
                }
            }
        }
        private void FrmDisplay_Load(object sender, EventArgs e)
        {
          
            foreach (ColumnHeader colh in skinListView1.Columns)
            {
                colh.Width = skinListView1.Width / skinListView1.Columns.Count;
            }
            var  s = (from a in _cfg.Specifications where a.PLCLocation==PLCLocation select a).ToList();
            for (int i = 0; i < s.Count; i++)
            {
                dataGridView1.Columns[2 * i + 1].HeaderText = s[i].Description;                
            }
            if (s.Count == 3)
            {
                lvHeader1 = s[0].Description;
                lvHeader2 = s[1].Description;
                lvHeader3 = s[2].Description;
                if(s[0].Addr_Standard!=0)
                {
                    lvHeader1Value = s[0].StandardValue.ToString("f3");
                }              
                else
                {
                    lvHeader1Value = "-";
                }
                if (s[1].Addr_Standard != 0)
                {
                    lvHeader2Value = s[1].StandardValue.ToString("f3");
                }
                else
                {
                    lvHeader2Value = "-";
                }
                if (s[2].Addr_Standard != 0)
                {
                    lvHeader3Value = s[2].StandardValue.ToString("f3");
                }
                else
                {
                    lvHeader3Value = "-";
                }
                if (s[0].Addr_Lower != 0)
                {
                    lvLowerOffsetValue1 = s[0].LowerOffset.ToString("f3");
                }
                else
                {
                    lvLowerOffsetValue1 = "-";
                }
                if (s[1].Addr_Lower != 0)
                {
                    lvLowerOffsetValue2 = s[1].LowerOffset.ToString("f3");
                }
                else
                {
                    lvLowerOffsetValue2 ="-";
                }
                if (s[2].Addr_Lower != 0)
                {
                    lvLowerOffsetValue3 = s[2].LowerOffset.ToString("f3");
                }
                else
                {
                    lvLowerOffsetValue3 = "-";
                }
                if (s[0].Addr_Upper != 0)
                {
                    lvUpperOffsetValue1 = s[0].UpperOffset.ToString("f3");
                }
                else
                {
                    lvUpperOffsetValue1 = "-";
                }
                if (s[1].Addr_Upper != 0)
                {
                    lvUpperOffsetValue2 = s[1].UpperOffset.ToString("f3");
                }
                else
                {
                    lvUpperOffsetValue2 = "-";
                }
                if (s[2].Addr_Upper != 0)
                {
                    lvUpperOffsetValue3 = s[2].UpperOffset.ToString("f3"); 
                }
                else
                {
                    lvUpperOffsetValue3 = "-";
                }
                      
            }
            else if (s.Count == 2)
            {
                lvHeader1 = s[0].Description;
                lvHeader2 = s[1].Description;
                lvHeader3 = "";
                if (s[0].Addr_Standard != 0)
                {
                    lvHeader1Value = s[0].StandardValue.ToString("f3");
                }
                else
                {
                    lvHeader1Value = "-";
                }
                if (s[1].Addr_Standard != 0)
                {
                    lvHeader2Value = s[1].StandardValue.ToString("f3");
                }
                else
                {
                    lvHeader2Value = "-";
                }
                if (s[0].Addr_Lower != 0)
                {
                    lvLowerOffsetValue1 = s[0].LowerOffset.ToString("f3");
                }
                else
                {
                    lvLowerOffsetValue1 = "-";
                }
                if (s[1].Addr_Lower != 0)
                {
                    lvLowerOffsetValue2 = s[1].LowerOffset.ToString("f3");
                }
                else
                {
                    lvLowerOffsetValue2 = "-";
                }
                if (s[0].Addr_Upper != 0)
                {
                    lvUpperOffsetValue1 = s[0].UpperOffset.ToString("f3");
                }
                else
                {
                    lvUpperOffsetValue1 = "-";
                }
                if (s[1].Addr_Upper != 0)
                {
                    lvUpperOffsetValue2 = s[1].UpperOffset.ToString("f3");
                }
                else
                {
                    lvUpperOffsetValue2 = "-";
                }
                Header3 = "";
                Header3Value = "";
                lvHeader3 = "";
                lvHeader3Value = "";
                lvLowerOffsetHeader3 = "";
                lvLowerOffsetValue3 = "";
                lvUpperOffsetHeader3 = "";
                lvUpperOffsetValue3 = "";
            }

            if (s.Count == 1)
            {
                TestItem1 = s[0].Description;

                TestItem2 = "";
                TestItem2NGQty = "";
                TestItem2NGRate = "";

                TestItem3 = "";
                TestItem3NGQty = "";
                TestItem3NGRate = "";
            }
            else if (s.Count == 2)
            {
                TestItem1 = s[0].Description;

                TestItem2 = s[1].Description;

                TestItem3 = "";
                TestItem3NGQty = "";
                TestItem3NGRate = "";
            }
            else if (s.Count == 3)
            {
                TestItem1 = s[0].Description;

                TestItem2 = s[1].Description;

                TestItem3 =s[2].Description;
            }

        }
    }
    public class SubItemNGRate
    {
         public string Name { get; set; }
         public string NgQty { get; set; }
         public string NgRate{get;set;}
    }
}
